﻿// -----------------------------------------------------------------------
// <copyright company="www.3WayWebdesign.de">
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
// 
// <author>Michael Morbach</author>
// <email>Michael@3waywebdesign.de</email>
// <web>http://www.3waywebdesign.de</web>
// ----------------------------------------------------------------------


using System;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Tww.Wpf.Controls.Controls.Shared
{
    public class VisualStates
    {
        #region  - Felder (öffentlich) -

        public const string GroupActive = "ActiveStates";
        public const string GroupBusyStatus = "BusyStatusStates";
        public const string GroupCalendarButtonFocus = "CalendarButtonFocusStates";
        public const string GroupCommon = "CommonStates";
        public const string GroupDecrease = "DecreaseStates";
        public const string GroupExpandDirection = "ExpandDirectionStates";
        public const string GroupExpansion = "ExpansionStates";
        public const string GroupFocus = "FocusStates";
        public const string GroupHasItems = "HasItemsStates";
        public const string GroupIncrease = "IncreaseStates";
        public const string GroupInteractionMode = "InteractionModeStates";
        public const string GroupLocked = "LockedStates";
        public const string GroupPopup = "PopupStates";
        public const string GroupSelection = "SelectionStates";
        public const string GroupValidation = "ValidationStates";
        public const string GroupVisibility = "VisibilityStates";
        public const string GroupWatermark = "WatermarkStates";
        public const string StateActive = "Active";
        public const string StateBubbleHide = "BubbleHide";
        public const string StateBubbleShow = "BubbleShow";
        public const string StateBusy = "Busy";
        public const string StateCalendarButtonFocused = "CalendarButtonFocused";
        public const string StateCalendarButtonUnfocused = "CalendarButtonUnfocused";
        public const string StateCollapsed = "Collapsed";
        public const string StateDecreaseDisabled = "DecreaseDisabled";
        public const string StateDecreaseEnabled = "DecreaseEnabled";
        public const string StateDisabled = "Disabled";
        public const string StateDisplay = "Display";
        public const string StateEdit = "Edit";
        public const string StateExpandDown = "ExpandDown";
        public const string StateExpandLeft = "ExpandLeft";
        public const string StateExpandRight = "ExpandRight";
        public const string StateExpandUp = "ExpandUp";
        public const string StateExpanded = "Expanded";
        public const string StateFocused = "Focused";
        public const string StateHasItems = "HasItems";
        public const string StateHidden = "Hidden";
        public const string StateIdle = "Idle";
        public const string StateInactive = "Inactive";
        public const string StateIncreaseDisabled = "IncreaseDisabled";
        public const string StateIncreaseEnabled = "IncreaseEnabled";
        public const string StateInvalidFocused = "InvalidFocused";
        public const string StateInvalidUnfocused = "InvalidUnfocused";
        public const string StateLocked = "Locked";
        public const string StateMaximized = "Maximized";
        public const string StateMinimized = "Minimized";
        public const string StateMouseOver = "MouseOver";
        public const string StateNoItems = "NoItems";
        public const string StateNormal = "Normal";
        public const string StatePopupClosed = "PopupClosed";
        public const string StatePopupOpened = "PopupOpened";
        public const string StatePressed = "Pressed";
        public const string StateReadOnly = "ReadOnly";
        public const string StateSelected = "Selected";
        public const string StateSelectedInactive = "SelectedInactive";
        public const string StateUnfocused = "Unfocused";
        public const string StateUnlocked = "Unlocked";
        public const string StateUnselected = "Unselected";
        public const string StateUnwatermarked = "Unwatermarked";
        public const string StateValid = "Valid";
        public const string StateVisible = "Visible";
        public const string StateWatermarked = "Watermarked";
        

        #endregion

        #region  - Methoden (öffentlich) -

        public static void GoToState(Control control, bool useTransitions, params string[] stateNames)
        {
            foreach (string name in stateNames)
            {
                VisualStateManager.GoToState(control, name, useTransitions);
            }
        }

        public static FrameworkElement GetImplementationRoot(DependencyObject dependencyObject)
        {
            return (VisualTreeHelper.GetChildrenCount(dependencyObject) == 1)
                ? VisualTreeHelper.GetChild(dependencyObject, 0) as FrameworkElement
                : null;
        }

        public static VisualStateGroup TryGetVisualStateGroup(DependencyObject dependencyObject, string groupName)
        {
            FrameworkElement root = GetImplementationRoot(dependencyObject);
            if (root == null)
            {
                return null;
            }

            // ReSharper disable once AssignNullToNotNullAttribute
            return
                VisualStateManager.GetVisualStateGroups(root)
                    .OfType<VisualStateGroup>()
                    .FirstOrDefault(group => string.CompareOrdinal(groupName, @group.Name) == 0);
        }

        #endregion
    }
}